package com.ruoyi.project.wenyanwen.generate.convert;


import com.ruoyi.common.utils.JsonUtil;
import com.ruoyi.project.wenyanwen.generate.domain.sqlite.ArticleSqlite;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jackyshi
 */
public class ArticleConvert extends AbstractConvert {

    @Override
    public List<ArticleSqlite> convert(List<?> list) {


        List<ArticleSqlite> l = new ArrayList<>();

        for (Object obj : list) {

            com.ruoyi.project.wenyanwen.article.domain.Article article = (com.ruoyi.project.wenyanwen.article.domain.Article) obj;
            ArticleSqlite model = new ArticleSqlite();
            model.setArticleId(article.getId().intValue());
            model.setName(article.getName());
            model.setSummary(article.getZhaiyao());
            model.setAuthor(article.getAuthor());
            model.setContent(zipAndencrypt(article.getContent()));
            if (StringUtils.isEmpty(article.getYiwenSentence()) || StringUtils.equalsIgnoreCase(article.getYiwenSentence(), "[]")) {
                model.setYiwen(zipAndencrypt(article.getYiwen()));
            }
            if (StringUtils.isEmpty(article.getZhujieSentence()) || StringUtils.equalsIgnoreCase(article.getZhujieSentence(), "[]")) {
                model.setZhujie(zipAndencrypt(article.getZhujie()));
            }
            model.setYiwenJson(zipAndencrypt(article.getYiwenSentence()));
            model.setZhujieJson(zipAndencrypt(article.getZhujieSentence()));
            model.setShangxi(zipAndencrypt(article.getShangxi()));
            model.setTemplate(article.getTemplate());
            model.setTags(getTags(article.getTag()));
            model.setSeq(article.getSeq().intValue());
            model.setCategory(article.getCategory());
            l.add(model);
        }

        return l;
    }

    private String getTags(String tag) {
        List<Integer> ids = new ArrayList<>();
        if (StringUtils.isNotBlank(tag)) {
            List<Object> datas = JsonUtil.toListObject(tag, Object.class);
            for (Object data : datas) {
                ids.add(Integer.parseInt(data.toString()));
            }
        }
        return JsonUtil.toJson(ids);
    }
}
